/*
 * Author : Priyanka & Lin
 */

package sg.edu.nus.iss.cerberus.wizard;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;

import sg.edu.nus.iss.cerberus.Activator;
import sg.edu.nus.iss.cerberus.util.CerberusConstants;

public abstract class NewClassyWizard extends Wizard implements INewWizard {

	private final IType element;
	private IType createdType;
	private IStructuredSelection selection;

	public NewClassyWizard(IType element) {
		this.element = element;
		setNeedsProgressMonitor(true);
		setWindowTitle(CerberusConstants.WINDOW_TITLE);
	}

	protected IType getType() {
		return element;
	}

	public IType open() {
		WizardDialog dialog = new WizardDialog(PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow().getShell(), this);
		if (dialog.open() == Window.OK) {
			typeCreated(createdType);
			return createdType;
		}
		creationAborted();
		return null;
	}

	/** To be overridden if needed */
	protected void typeCreated(IType createdType2) {
	}

	/** To be overridden if needed */
	protected void creationAborted() {
	}

	public void setSelection(IStructuredSelection selection) {
		this.selection = selection;
	}

	public IStructuredSelection getSelection() {
		return this.selection;
	}

	public boolean performFinish() {
		try {
			createdType = createClass();
		} catch (Exception e) {
        	e.printStackTrace();

		}
		return true;
	}

	public IDialogSettings getDialogSettings() {
		return Activator.getDefault().getDialogSettings();
	}

	public void init(IWorkbench workbench, IStructuredSelection selection) {
	}

	public abstract IType createClass() throws CoreException,
			InterruptedException;

	protected abstract IPackageFragmentRoot getPackageFragmentRoot();

	protected IPackageFragmentRoot getSourceFolderForUnitTest() {
		String key = getPackageFragmentRootKey();
		String root = getDialogSettings().get(key);
		IPackageFragmentRoot fragment = (IPackageFragmentRoot) JavaCore
				.create(root);

		if (fragment != null && fragment.exists()) {
			return fragment;

		}
		return getPackageFragmentRootFromType();
	}

	private IPackageFragmentRoot getPackageFragmentRootFromType() {
		return (IPackageFragmentRoot) getType().getAncestor(
				IJavaElement.PACKAGE_FRAGMENT_ROOT);
	}

	/**
	 * @Description : Gets target package name
	 */
	protected IPackageFragment createPackageFragment(String targetPackage)
			throws JavaModelException {
		return getType().getJavaProject().getPackageFragmentRoots()[0]
				.getPackageFragment(targetPackage);
	}

	private String getPackageFragmentRootKey() {
		return getClass().getName() + ".packageFragmentRoot";
	}
}